﻿using Furion;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using iemtm.WeChat.Core.Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace iemtm.WeChat.Core.Managers
{
    public class UserManager : IUserManager, IScoped
    {
        private readonly IRepository<UserAccount> _userAccountRepository;
        public UserManager(IRepository<UserAccount> userAccountRepository)
        {
            _userAccountRepository = userAccountRepository;
        }


        

        public async Task<UserAccount> CheckUserAsync(int userId)
        {
            return await _userAccountRepository.FirstOrDefaultAsync(u => u.Id == userId);
        }

        public async Task<UserAccount> CheckUserAsync(string UserCode)
        {
            return await _userAccountRepository.FirstOrDefaultAsync(u => u.UserCode == UserCode);
        }

        public async Task<UserAccount> CheckUserAsync(string UserCode, string UserPwd)
        {
            return await _userAccountRepository.FirstOrDefaultAsync(u => u.UserCode == UserCode&&u.UserPwd== UserPwd);
        }

        public async Task<UserAccount> GetUserByWxOpenIdAsync(string WxOpenId)
        {
            return await _userAccountRepository.FirstOrDefaultAsync(u => u.WxOpenId == WxOpenId);
        }

        public async Task<bool> BindWeChat(UserAccount user,string WxOpenId,string WxHreadImg)
        {
            user.WxOpenId = WxOpenId;
            user.WxHreadImg = WxHreadImg;
            var UpdateUser = await _userAccountRepository.UpdateIncludeAsync(user, new[] { "WxOpenId", "WxHreadImg" });
            return true;
        }
        public async Task<bool> UNBindWeChat(UserAccount user)
        {
            user.WxOpenId = null;
            user.WxHreadImg = null;
            var UpdateUser = await _userAccountRepository.UpdateIncludeAsync(user, new[] { "WxOpenId", "WxHreadImg" });
            return true;
            
        }

        ///// <summary>
        ///// 用户可见区域过滤
        ///// </summary>
        ///// <param name="selAreaCode"></param>
        ///// <param name="logininfo"></param>
        ///// <param name="tablePrex"></param>
        ///// <returns></returns>
        //public  string GetAreaFilterStrWx(string selAreaCode, UserAccount logininfo, string tablePrex = "")
        //{

        //    //用户可见区域过滤
        //    List<string> visibleArea = logininfo.VisibleArea.StringSplitTirm(",").ToList();
        //    if (visibleArea == null || visibleArea.Count <= 0)
        //        return null;
        //    //用户选择区域过滤
        //    List<string> visibleAreaSql = (from ii in visibleArea where selAreaCode.StartsWith(ii) select tablePrex + "AreaCode like '" + selAreaCode + "%'").Distinct().ToList();
        //    if (visibleAreaSql == null || visibleAreaSql.Count <= 0)
        //        visibleAreaSql = (from ii in visibleArea where ii.StartsWith(selAreaCode) select tablePrex + "AreaCode like '" + ii + "%'").ToList();
        //    if (visibleAreaSql == null || visibleAreaSql.Count <= 0)
        //        return null;
        //    return "(" + visibleAreaSql.StrListToStr(" or ") + ")";
        //}
    }
    
}
